<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.jxgy_delete"
          @click="handleDelete"
          >删 除
        </el-button>
        <UploadStyle
          ref="uploadStyle"
          v-if="screenShotDialogVisible"
          :visible="screenShotDialogVisible"
          :img-url="imgUrl"
          :proportion="proportion"
          @closeDialog="closeDialog"
          @getImgURL="getImgURL"
        />
      </template>
      <template slot-scope="{ type, size, row, index }" slot="menu">
        <el-button
          type="text"
          size="small"
          @click="handleEditState({ type, size, row, index })"
          >修改状态</el-button
        >
      </template>
      <!-- <template slot="content" slot-scope="scope">
        <div v-html="scope.row.content"></div>
      </template> -->
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getOutlineList,
  eidtOraddlistOutline,
  editOutlineStatus,
  deleteOutline,
} from "@/api/service/studydata";

import { mapGetters } from "vuex";

export default {
  data() {
    return {
      screenShotDialogVisible: false,
      imgUrl: "",
      backdone: null,
      proportion: [166, 108], // 宽高的像素数组
      backuploadLoading: null,
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "auto",
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        menuType: "menu",
        menuBtnTitle: "更多操作",
        column: [
          {
            label: "学校名称",
            prop: "schoolId",
            type: "tree",
            search: true,
            overHidden: true,
            cascader: ["xqId"],
            rules: [
              {
                required: true,
                message: "请选择学校",
                trigger: "blur",
              },
            ],
            dicUrl: "/api/ManageCourseSet/GetSchoolSelect",
            props: {
              label: "title",
              value: "id",
            },
          },
          {
            label: "学期",
            prop: "xqId",
            overHidden: true,
            rules: [
              {
                required: true,
                message: "请选择学期",
                trigger: "blur",
              },
            ],
            type: "select",
            dicUrl: "/api/ManageCourseSet/GetSemesterSelect?schoolId={{key}}",
            props: {
              label: "title",
              value: "id",
            },
            formatter: row=>{
              return row.xqName
            }
          },
          {
            label: "所属阶段",
            prop: "jdId",
            rules: [
              {
                required: true,
                message: "请选择所属阶段",
                trigger: "blur",
              },
            ],
            hide: true,
            type: "select",
            dicUrl: "/api/PcPublicService/GetSchoolStageSelect",
            cascader: ["njId"],
            props: {
              label: "title",
              value: "id",
            },
          },

          {
            label: "年级",
            prop: "njId",
            type: "select",
            overHidden: true,
            rules: [
              {
                required: true,
                message: "请输入年级名称",
                trigger: "blur",
              },
            ],
            dicUrl:
              "/api/ManageCourseSet/GetGradeSelect?jdId={{jdId}}&schoolId={{schoolId}}",
            props: {
              label: "title",
              value: "id",
            },
            cascader: ["bjId"],
            formatter: row=>{
              return row.njName
            }
          },
          {
            label: "班级",
            prop: "bjId",
            overHidden: true,
            rules: [
              {
                required: true,
                message: "请输入班级",
                trigger: "blur",
              },
            ],
            type: "tree",
            multiple: true,
            dicUrl: "/api/ManageStudent/GetClassSelect?njId={{key}}",
            props: {
              label: "bjName",
              value: "id",
            },
            cascader: ["kcId"],
            formatter: (row) => {
              return row.bjName;
            },
          },
          {
            label: "学科名称",
            prop: "kcId",
            overHidden: true,
            rules: [
              {
                required: true,
                message: "请选择学科名称",
                trigger: "blur",
              },
            ],
            type: "select",
            dicUrl:
              "/api/ManageCourseSet/GetCourseSelect?xqId={{xqId}}&bjIds={{bjId}}",
            props: {
              label: "title",
              value: "id",
            },
            formatter: row=>{
              return row.kcName
            }
          },
          {
            label: "教学大纲",
            component: "avueUeditor",
            prop: "content",
            overHidden: true,
            hide: true,
            width: 500,
            action: "/api/localfiles/uploadPcFile",
            headers: {},
            data: {},
            propsHttp: {
              url: "link",
              name: "name",
              res: "data",
            },
            span: 24,
          },
          {
            label: "是否启用",
            prop: "status",
            type: "switch",
            value: 0,
            dicData: [
              {
                label: "未启用",
                value: 0,
              },
              {
                label: "启用",
                value: 1,
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission", "JSuserInfo"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.jxgy_add, false),
        viewBtn: this.vaildData(this.permission.jxgy_view, false),
        delBtn: this.vaildData(this.permission.jxgy_delete, false),
        editBtn: this.vaildData(this.permission.jxgy_edit, false),
      };
    },
    schoolId() {
      return this.JSuserInfo.schoolId;
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    rowSave(row, done, loading) {
      row.bjId = row.bjId && row.bjId.join()
      row.id = row.id === "" ? "0" : row.id;
      eidtOraddlistOutline(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      eidtOraddlistOutline(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return deleteOutline({ ids: row.id });
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return deleteOutline({ ids: this.ids });
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      const bj = this.findObject(this.option.column, "bjId");
      type == 'edit' ? bj.multiple = false : bj.multiple = true;
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      // params.SchoolId = this.schoolId
      // params.tenant_id = ''
      getOutlineList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    // 修改状态
    handleEditState({ row }) {
      this.$confirm("确定修改启用状态?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        editOutlineStatus({ id: row.id }).then((res) => {
          if (res.data.code == 200) {
            this.$message({type: 'success', message: res.data.msg || "操作成功"})
          }
        }).then(() => {
          this.query = {}
          this.onLoad(this.page);
        })
      })
    },
    closeDialog(val) {
      if (this._uploadLoading !== null) {
        this._uploadLoading();
        this._uploadLoading = null;
      }
      this.screenShotDialogVisible = val;
    },
    getImgURL(data) {
      if (this._done !== null) {
        this._done(data);
        this._done = null;
      }
      this.closeDialog(false);
    },
  },
};
</script>

<style>
</style>
